public class Bank implements Runnable{
    int money = 200;
    public void  run(){
        if (Thread.currentThread().getName().equals("会计")){
            saveOrTake(500,false);
        } else if (Thread.currentThread().getName().equals("出纳")) {
            saveOrTake(200,true);
        }
    }
    private synchronized void saveOrTake(int amount,boolean flag){
        if (flag){
            for (int i=1;i<=4;i++){
                money -= amount/4;
                System.out.println("取了一笔，还剩"+money);
                try{
                    Thread.sleep(1000);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }else {
            for(int i=1;i<=2;i++){
                money += amount/2;
                System.out.println("存了一笔，还剩"+money);
                try {
                    Thread.sleep(1000);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
